package com.caimore.module.m35;

import com.caimore.common.Serial_For_Smslib;
import com.caimore.common.SmsUtils_sdk;
import com.caimore.entity.SMSMessageMgr;
import com.caimore.entity.SendSmsParam;
import com.caimore.modulefactory.IsmsWriter;
import java.util.ArrayList;

public class ATWriterThreadPduM35 implements IsmsWriter {

   Serial_For_Smslib SerilaSmslib;
   private volatile boolean shutdown = false;
   private volatile boolean sendMs = false;
   static final int PDUlen = 64;
   static final int i7bitLen = 134;
   Thread t1 = null;


   public ATWriterThreadPduM35(Serial_For_Smslib _smslib_test, boolean _shutdown) {
      this.SerilaSmslib = _smslib_test;
      this.shutdown = _shutdown;
   }

   public void start() {
      this.t1 = new Thread() {
         public void run() {
            String e;
            String lstSendContext;
            char[] i;
            try {
               e = "AT+CPIN ? \r";
               char[] sReceivedNum = e.toCharArray();
               ATWriterThreadPduM35.this.SerilaSmslib.writeByte(sReceivedNum);
               System.out.println("[ATWriterThreadPduM35]sSend-AT-Command:" + e);

               while(!SMSMessageMgr.getInstance().getREADY() && !SMSMessageMgr.getInstance().getREADY()) {
                  ;
               }

               if(SMSMessageMgr.getInstance().getREADY()) {
                  for(int oSendSmsParam = 0; oSendSmsParam < 2; ++oSendSmsParam) {
                     if(oSendSmsParam == 0) {
                        lstSendContext = "AT+CNMI=1,0,0,0,1\r";
                        i = lstSendContext.toCharArray();
                        ATWriterThreadPduM35.this.SerilaSmslib.writeByte(i);
                        System.out.println("[ATWriterThreadPduM35]sSend-AT-Command:" + lstSendContext);
                        Thread.sleep(500L);
                     } else if(oSendSmsParam == 1) {
                        lstSendContext = "AT+CSCA ? \r";
                        i = lstSendContext.toCharArray();
                        ATWriterThreadPduM35.this.SerilaSmslib.writeByte(i);
                        System.out.println("[ATWriterThreadPduM35]sSend-AT-Command:" + lstSendContext);
                        Thread.sleep(500L);
                     }
                  }
               }
            } catch (Exception var21) {
               System.out.println(var21.getMessage());
            }

            while(!ATWriterThreadPduM35.this.shutdown) {
               try {
                  if(SMSMessageMgr.getInstance().getREADY()) {
                     e = null;
                     String var22 = null;
                     SendSmsParam var23 = SMSMessageMgr.getInstance().delSendSmsParam();
                     if(var23 != null) {
                        e = var23.getContent();
                        var22 = var23.getPhone();
                     }

                     if(e == null && var22 == null && !ATWriterThreadPduM35.this.sendMs) {
                        lstSendContext = "AT+CMGL=0\r";
                        i = lstSendContext.toCharArray();
                        ATWriterThreadPduM35.this.SerilaSmslib.writeByte(i);
                        System.out.println("[ATWriterThreadPduM35]sSend-AT-Command:" + lstSendContext);
                        var23 = SMSMessageMgr.getInstance().delSendSmsParam();
                        if(var23 != null) {
                           e = var23.getContent();
                           var22 = var23.getPhone();
                        }
                     } else {
                        ATWriterThreadPduM35.this.sendMs = true;
                     }

                     ArrayList var24 = new ArrayList();
                     if(e != null) {
                        if(e.length() <= 64) {
                           var24.add(e);
                        } else {
                           while(e.length() > 64) {
                              String var25 = e.substring(0, 64);
                              var24.add(var25);
                              e = e.substring(64);
                              if(e.length() <= 64) {
                                 var24.add(e);
                              }
                           }
                        }

                        int var26 = 0;

                        while(var26 < var24.size()) {
                           String sTempBody = (String)var24.get(var26);
                           String sSeriNo = var26 + 1 < 9?"0" + (var26 + 1):"" + (var26 + 1);
                           String sAllNo = var24.size() < 9?"0" + var24.size():"" + var24.size();
                           System.out.println("[ATWriterThreadPduM35]sAllNo: " + sAllNo + " , sSeriNo: " + sSeriNo);
                           char[] chSendContext = null;

                           for(int j = 0; j < 3; ++j) {
                              System.out.println("SmsUtils.getInstance().getOK():" + SMSMessageMgr.getInstance().getOK());
                              System.out.println("SmsUtils.getInstance().getCR():" + SMSMessageMgr.getInstance().getCR());
                              String newOrder;
                              if(j == 0) {
                                 newOrder = "AT+CMGF=0\r";
                                 char[] sContextHeader = newOrder.toCharArray();
                                 ATWriterThreadPduM35.this.SerilaSmslib.writeByte(sContextHeader);
                                 System.out.println("[ATWriterThreadPduM35]sSend-AT-Command:" + newOrder);
                                 Thread.sleep(2000L);
                              } else if(j == 1) {
                                 newOrder = SMSMessageMgr.getInstance().getCSCA();

                                 while(newOrder == null) {
                                    newOrder = SMSMessageMgr.getInstance().getCSCA();
                                    if(newOrder != null) {
                                       break;
                                    }
                                 }

                                 String var27 = "91" + SmsUtils_sdk.addFandSwap(newOrder);
                                 if(var27.length() / 2 >= 10) {
                                    var27 = var27.length() / 2 + var27;
                                 } else {
                                    var27 = "0" + var27.length() / 2 + var27;
                                 }

                                 String sSendContextBody = null;
                                 String sTempReceivedNum = SmsUtils_sdk.sendReceivedNum(var22);
                                 System.out.println(" [ATWriterThreadPduM35] SmsUtils.isUCS2(sTempBody) ");
                                 byte[] bySendContextPDU = SmsUtils_sdk.encodeUCS2_005100(sTempBody);
                                 sSendContextBody = SmsUtils_sdk.byte2hex((byte)(bySendContextPDU.length + 6)) + "050003" + sAllNo + sAllNo + sSeriNo + SmsUtils_sdk.bytesToHexString(bySendContextPDU);
                                 sSendContextBody = sTempReceivedNum + "000800" + sSendContextBody;
                                 String sContextTemp = var27 + sSendContextBody;
                                 chSendContext = sContextTemp.toCharArray();
                                 String atCommand = "AT+CMGS=" + sSendContextBody.length() / 2 + "\r";
                                 char[] atOrder = atCommand.toCharArray();
                                 ATWriterThreadPduM35.this.SerilaSmslib.writeByte(atOrder);
                                 System.out.println("[ATWriterThreadPduM35]sSend-ContextHeader:" + atCommand);
                                 Thread.sleep(2000L);
                              } else if(j == 2) {
                                 if(chSendContext != null) {
                                    char[] var28 = new char[chSendContext.length + 1];
                                    System.arraycopy(chSendContext, 0, var28, 0, chSendContext.length);
                                    var28[chSendContext.length] = 26;
                                    ATWriterThreadPduM35.this.SerilaSmslib.writeByte(var28);
                                    System.out.println("[ATWriterThreadPduM35]sSend-ContextBody:" + new String(chSendContext));
                                    System.out.println("\r\n\r\n");
                                    Thread.sleep(2000L);
                                 }

                                 ++var26;
                              }
                           }

                           Thread.sleep(2000L);
                        }

                        ATWriterThreadPduM35.this.sendMs = false;
                     }
                  }

                  Thread.sleep(2000L);
               } catch (InterruptedException var19) {
                  Thread.currentThread().interrupt();
               } catch (Exception var20) {
                  System.out.println(var20.getMessage());
               }
            }

         }
      };
      this.t1.start();
   }

   public final void shutdownRequest() {
      this.shutdown = true;
      this.t1.interrupt();
   }
}
